

/**
 * 创建entity Circle
 */
import Entity from '../Entity'

const DEFAULT_STYLE = {
  material: Cesium.Color.DEEPSKYBLUE.withAlpha(.7),
  clampToGround: true
}

export default class Circle extends Entity {

  /**
   * 创建一个Circle实体对象
   * @param {*} position 
   * @param {*} radius 
   * @param {*} options 
   */
  constructor(positions, radius, options = {}) {
    super()
    this._radius = radius
    this._positions = positions
    this._type = 'circle'
    this._style = {
      ...DEFAULT_STYLE,
      ...options
    }
    this.delegate = new Cesium.Entity({ ellipse: {} })

    this.mounted()
  }

  /**
  * 初始化对象属性
  */

  mounted() {
    this.positions = this._positions
    this.style = this._style
    this.radius = this._radius
  }

  /**
   * 旋转
   * @param {*} angle 
   */

  startRotate(angle = 1) {
    this.pitch = angle
    this.delegate.ellipse.rotation = new Cesium.CallbackProperty((time, result) => {
      this.rotateAmount += this.pitch
      if (this.rotateAmount > 360 || this.rotateAmount < -360) {
        this.rotateAmount = 0
      }
      this.heading = this.rotateAmount * Math.PI / 180
      return this.heading
    })
  }

  /**
   * 停止旋转
   */

  stopRotate() {
    this.delegate.ellipse.rotation = new Cesium.CallbackProperty((time, result) => {
      return this.heading
    })
  }

  /** 
  * @param {*} style Circle的样式
  * @returns 
  */

  setStyle(style) {
    if (!style) return
    delete style['position']
    Object.assign(this._style, { ...style })
    Object.assign(this.delegate.ellipse, { ...this._style })
  }

  /**
   * 转换为json文件供加载使用
   */
  toGeoJSON(){
    let id = this._id
    let type = this._type
    let positions = this._positions
    let radius = this._radius
    let orientation = {
      heading : this._heading,
      pitch : this._pitch,
      roll : this._roll
    }

    let color = undefined ,outlineColor = undefined
    if(this.style.material){
      color = [this.style.material.red , this.style.material.green , this.style.material.blue ,this.style.material.alpha]
      delete this._style['material']
    }

    let style = {
      color,
      ...this._style
    }
    let geojson = {id, type, radius, positions, orientation, style}

    var content = JSON.stringify(geojson)
    var blob = new Blob([content], { type: "text/plain;charset=utf-8" })
    saveAs(blob, "circle.json")

    return geojson
  }

  /**
   * 从geojson创建entity
   * @param {*} json 
   */
  static fromGeoJSON(json) {
    let material = undefined
    let jsonStyle = json.style
    if(jsonStyle.color){
      material = new Cesium.Color(jsonStyle.color[0] , jsonStyle.color[1] , jsonStyle.color[2] , jsonStyle.color[3])
      delete jsonStyle['color']
    }
    let positions = new Cesium.Cartesian3(json.positions.x , json.positions.y , json.positions.z)
    let radius = json.radius
    
    let style = {
      material,
      ...jsonStyle
    }
    
    return new Circle(positions , radius , {
      ...style
    })
  }

  get style() {
    return this._style
  }

  set style(val) {
    Object.assign(this._style, { ...val })
    Object.assign(this.delegate.ellipse, { ...this._style })
  }

  get positions() {
    return this._positions
  }

  set positions(val) {
    this._positions = val
    this.delegate.position = this._positions
  }

  get radius() {
    return this._radius
  }

  set radius(val) {
    this._radius = val
    this.delegate.ellipse.semiMajorAxis = this.radius
    this.delegate.ellipse.semiMinorAxis = this.radius
  }
}
